package com.wantupai.app.base;

import android.app.Application;
import android.content.Context;

import com.squareup.leakcanary.LeakCanary;
import com.wantupai.app.BuildConfig;
import com.wantupai.app.R;
import com.wantupai.app.crash.CaocConfig;
import com.wantupai.app.main.MainActivity;
import com.wantupai.app.utils.AppUtils;

import cn.leo.magic.screen.MagicScreenAdapter;

public class AppSingleton {

    private Context mContext;
    private Application mApplication;

    private AppSingleton() {

    }

    private static class Singleton {
        private static AppSingleton instance = new AppSingleton();
    }

    public static AppSingleton getInstance() {
        return Singleton.instance;
    }

    public void init(Application application) {
        mApplication = application;
        mContext=mApplication;
        init();
    }

    private void init() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                AppUtils.init(mApplication);
                initCrash();
                if (BuildConfig.DEBUG) {
                    //内存泄漏检测上线后记得注释
                    if (!LeakCanary.isInAnalyzerProcess(mContext)) {
                        LeakCanary.install(mApplication);
                    }
                }
                //今日头条适配方案
                MagicScreenAdapter.initDesignWidthInDp(360);
            }
        }).start();

    }

    /**
     * 检测崩溃日志
     */
    private void initCrash() {
        CaocConfig.Builder.create()
                .backgroundMode(CaocConfig.BACKGROUND_MODE_SILENT) //背景模式,开启沉浸式
                .enabled(true) //是否启动全局异常捕获
                .showErrorDetails(true) //是否显示错误详细信息
                .showRestartButton(true) //是否显示重启按钮
                .trackActivities(true) //是否跟踪Activity
                .minTimeBetweenCrashesMs(2000) //崩溃的间隔时间(毫秒)
                .errorDrawable(R.mipmap.ic_launcher) //错误图标
                .restartActivity(MainActivity.class) //重新启动后的activity
//                .errorActivity(YourCustomErrorActivity.class) //崩溃后的错误activity
                .apply();
    }

}

